﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Koala.Pro.Core.Dto;
using Koala.Pro.Core.Models;
using Koala.Pro.Helpers;
using Koala.Pro.Services;

namespace Koala.Pro.ViewModels
{
    public partial class OperationLogSettingListViewModel : BaseListViewModel<OperationLogSettingInfo, string, OperationLogSettingPagedDto>
    {
        [ObservableProperty]
        private TreeViewModel<CustomTreeNode> treeModel = new TreeViewModel<CustomTreeNode>();

        public OperationLogSettingListViewModel(IOperationLogSettingService service)
            : base(service)
        {
            ExportFields = "Forbid,TableName,InsertLog,DeleteLog,UpdateLog,Note,Creator,CreateTime";
        }

        [RelayCommand]
        private async Task GetTree()
        {
        }

        private List<CustomTreeNode> AddRecursive(List<FunctionInfo> children, CustomTreeNode parentNode = null)
        {
            List<CustomTreeNode> list = new List<CustomTreeNode>();
            foreach (FunctionInfo child in children)
            {
                CustomTreeNode customTreeNode = new CustomTreeNode
                {
                    Name = child.Name,
                    Id = child.Id
                };
                AddRecursive(child.Children, customTreeNode);
                list.Add(customTreeNode);
            }
            if (parentNode != null)
            {
                parentNode.Children = list;
                return new List<CustomTreeNode> { parentNode };
            }
            return list;
        }
    }
}
